/**
 * WTF Engine
 * 
 * License... etc.
 **
 * Particle System
 **
 * Authors: Sean Micklethwaite
 */

#include "fx_particle.h"
#include "pmodel.h"

using namespace WtfEngine;

FxParticle::FxParticle(const tCreationOptions& options):
Physical(options, options.rGraphicsData, new PhysicalModels::Point("", options.Mass), true), mLife(options.Life) {
	this->setPosition(options.vPosition);
	this->setVelocity(options.vVelocity);

	IDynamic::Init();
};

FxParticle::~FxParticle() {

};

void	FxParticle::Update() {
	this->Update(Kernel::GetInstance().GetGameState()->GetDelta());
};

void	FxParticle::Destroy() {
	IDynamic::Destroy();
	Physical::Destroy();
};


/**
 * Destroys the particle when its life reaches zero.
 **/
void	FxParticle::Update(tGameTime dt) {
	mLife -= dt;
	if(mLife <= 0) {
		Destroy();
	};
};



/**** Particle Generator ****/

FxParticleGenerator::FxParticleGenerator() {
};
FxParticleGenerator::~FxParticleGenerator() {
};

void FxParticleGenerator::Init(const tInitOptions& options) {
	mOptions = options;
	mParticleTimer = 0;

	Animation::Init();
};

void FxParticleGenerator::GenerateParticle(const WtfGraphics::GraphicsData::tCacheRef& data) {
	new FxParticle(FxParticle::tCreationOptions(data,
		mOptions.ParticleLife + (getRandomFloat() * mOptions.RandomComponentOfLife),
		mOptions.vPosition,
		mOptions.vDirection * mOptions.Speed,
		mOptions.Mass));
};

void FxParticleGenerator::Update(tGameTime dt) {
	if(mOptions.GeneratorLife >= 0) {
		// Age the generator, destroy it when expired
		mOptions.GeneratorLife -= dt;
		if(mOptions.GeneratorLife <= 0) {
			Destroy();
			return;
		};
	};

	// Generate particle if interval expired
	mParticleTimer -= dt;
	if(mParticleTimer <= 0) {
		(*mOptions.rGeneratorFunc)();
		mParticleTimer = mOptions.ParticleInterval;
	};
};

void FxParticleGenerator::Destroy() {

};

